package so.shanku.essential.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import net.tsz.afinal.json.JsonMap;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;
import so.shanku.essential.R;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.utils.ImageLoaderBitmapCallBack;
import striveen.util.used.view.view.HorizontalListView;

import java.util.List;
import java.util.Map;

/**
 * Created by Jaelyn on 2015/9/7.
 */
public class ManBrandHotAdapter extends MyBaseAdapter {
	private RecommandProductAdapter manBrandHLVAdapter;
	private ItemAttentionChange attentionChange;
	private List<JsonMap<String, Object>> dataHLV;//每个item的数据
	private List<JsonMap<String, Object>> dataList;
	public ManBrandHotAdapter(Context context, List<? extends Map<String, ?>> datas, ItemAttentionChange callback2) {
		super(context, datas);
		this.attentionChange = callback2;
	}


	@Override
	public int getCount() {
		return datas.size();
	}

	@Override
	public View getView(final int position, View view, ViewGroup viewGroup) {
		final ViewHolder viewHolder;
		if (view == null) {
			view = layoutInflater.inflate(R.layout.item_manbrand_list, viewGroup, false);
			viewHolder = new ViewHolder(view);
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) view.getTag();
		}
		final JsonMap data = (JsonMap) datas.get(position);      //brandLog
		viewHolder.tv_name.setText(data.getStringNoNull("ShopName"));
		viewHolder.tv_msg.setText(data.getStringNoNull("BusinessCategory"));
		String vendorId = data.getStringNoNull("VendorId");
		String headlog = data.getStringNoNull("brandLog");
		if (MyApplication.getInstance().getBitmapHashMap().containsKey(headlog)) {
			viewHolder.iv_head.setImageBitmap((MyApplication.getInstance().getBitmapHashMap().get(headlog)));
		} else {
			ImageLoader.getInstance().displayImage(headlog,viewHolder.iv_head, MyApplication.getInstance().getBrandDefaultOption(), new ImageLoaderBitmapCallBack());
		}
		boolean isSelected = MyApplication.getInstance().validateAttendBrands(vendorId);
		viewHolder.tv_if_attention.setSelected(isSelected);
		if (isSelected) {
			viewHolder.tv_if_attention.setText(R.string.attentioned);
		} else {
			viewHolder.tv_if_attention.setText(R.string.attention);
		}
		dataHLV = data.getList_JsonMap("ProductList");

		manBrandHLVAdapter = new RecommandProductAdapter(context);
		manBrandHLVAdapter.setDatas(dataHLV);
		manBrandHLVAdapter.setFlag(1);

		viewHolder.tv_if_attention.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (attentionChange != null) {
					attentionChange.onStatusChange(position, viewHolder.tv_if_attention);
				}
			}
		});
		viewHolder.hlv_manbrandList.setAdapter(manBrandHLVAdapter);
		viewHolder.hlv_manbrandList.setFocusable(false);
		viewHolder.hlv_manbrandList.setClickable(true);
		viewHolder.hlv_manbrandList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				dataList = data.getList_JsonMap("ProductList");
                dataList.get(position).getStringNoNull("ProductId");

			}
		});
		return view;
	}

	private static class ViewHolder {
		@ViewInject(R.id.iv_head)
		private ImageView iv_head;
		@ViewInject(R.id.tv_name)
		private TextView tv_name;
		@ViewInject(R.id.tv_msg)
		private TextView tv_msg;
		@ViewInject(R.id.tv_if_attention)
		private TextView tv_if_attention;
		@ViewInject(R.id.hlv_manbrandList)
		private HorizontalListView hlv_manbrandList;

		public ViewHolder(View view) {
			ViewUtils.inject(this, view);
		}
	}
	public interface ItemAttentionChange{
		void onStatusChange(int position,TextView tv_if_attention);
	}
}
